package first.endtoend

/**
 * Created with IntelliJ IDEA.
 * User: Olfa
 */
class Level
{


    String name            //Zone, States or UT, Region Or Division, District
    Date dateCreated
    Date lastUpdated
    static hasOne = [parentLevel: Level]
    static hasMany = [localities: Locality, admins: Admin, childrenLevel: Level]

    static constraints = {
        name(nullable: false, unique: true)
        dateCreated nullable: true
        lastUpdated nullable: true
        parentLevel nullable: true
        localities nullable: true
        admins nullable: true
        childrenLevel nullable: true

    }

    @Override
    public String toString()
    {
        return name
    }

    /**
     * Recursive method for getting all children of a node
     * @param level The Current Level
     * @param resp The list of Cildren
     * @return All the children of the node
     */
    public ArrayList<Level> getAllChildrenLevel(Level level, def resp)
    {
        if (resp.contains(level) == false)
        {
            resp.add(level)
        }
        for (Level l : level.childrenLevel)
        {
            this.getAllChildrenLevel(l, resp)
        }

        return resp
    }


}
